//Page Object
Page({
  data: {
    jujialist: [],
    userInfo: ''
  },
  //获取缓存
  onLoad() {
    let user = wx.getStorageSync('user')
    console.log('进入小程序的index页面获取缓存', user)
    this.setData({
      userInfo: user
    })
    this.getjujiaList()
  },
  //授权登录
  login() {
    wx.getUserProfile({
      desc: '必须授权才可以使用',
      success: res => {
        let user = res.userInfo
        wx.setStorageSync('user', user) //把用户信息缓存到本地
        console.log('用户信息', user)
        this.setData({
          userInfo: user
        })
      },
      fail: res => {
        console.log('授权失败', res)
      }
    })
  },
  // 跳转个人信息
  shezhi:function(e){
    console.log("shezhi");
    wx.navigateTo({
      url: '../shezhi/shezhi',
    })
  },
  getjujiaList() {
    wx.showLoading({
      title: '加载中...',
    })
    let len = this.data.jujialist.length
    console.log("当前长度", len);
    wx.cloud.database().collection("jujiadata")
      .skip(len) //分页器
      .get()
      .then(res => {
        wx.hideLoading()
        console.log("商品列表", res)
        let dataList = res.data
        if (dataList.length <= 0) {
          // wx.showToast({
          //   title: '没有更多数据了',
          //   icon: 'none',
          // })
        }
        this.setData({
          jujialist: this.data.jujialist.concat(res.data),
        })
        console.log(res.data);
      })
      .catch(res => {
        wx.hideLoading()
        console.log("请求失败", res);
      })
  },
   // 跳详情
   goDetail(e) {
    // console.log("点击了", e.currentTarget.dataset.item)
    let id = e.currentTarget.dataset.item._id
    console.log(e.currentTarget.dataset.item._id);
    wx.navigateTo({
      url: '/pages/detail/detail?id=' + id,
    })
  },

})